(function(angular) {
	'docTemplate strict';
	angular.module('fs.waf').controller('cda.docTemplate.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService','storageService', 'docTemplateService' ];
	function deleteController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, storageService, docTemplateService) {
		var ctrl = this, docTemplateInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			//init dTempParms
//			docTemplateService.initParmOptions($scope, ctrl);
			ctrl.innerPreGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPreParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPreGridOptions.data = 'vm.docTemplate.docTempPreParms';
			ctrl.innerPostGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPostParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPostGridOptions.data = 'vm.docTemplate.docTempPostParms';
			ctrl.innerPreGridOptions.enableRowHeaderSelection = false;
			//postParms
			ctrl.innerPostGridOptions.enableRowHeaderSelection = false;

			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			// init docTemplate Parameter
			docTemplateService.initDataOptions($scope, ctrl);
			
			authService.delete(docTemplateInfo).then(function(response) {
				ctrl.docTemplate = response.data;
				docTemplateService.setDocPrePoParm($scope, ctrl);
				
			});
		};
		
		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.docTemplate.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.docTemplate).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.docTemplate.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.doctemplate.list',
					'state' : 'cda.docTemplate.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		};
		
	};
})(angular);
